import Vue from 'vue';
import VueRouter,{RouteConfig} from 'vue-router';
import {DocConfig,LastDocConfig} from './define';
import Main from '../Main.vue';
import pc from './pc';
import app from './app';

Vue.use(VueRouter);

function getRoutes(arr:Array<DocConfig|LastDocConfig>,rootName:string):Array<RouteConfig>{
	const routes:Array<RouteConfig>=[];
	for(const config of arr){
		const children=(config as DocConfig).children;
		if(Array.isArray(children)){
			const path=rootName+config.value+'/';
			routes.push(...getRoutes(children,path));
		}else{
			const lastConfig=(config as LastDocConfig);
			let path=rootName+lastConfig.value;
			if(lastConfig.common){
				path=path.replace(/^\w+\//,'common/');
			}
			const absolutePath='/'+path;
			lastConfig.path=absolutePath;
			routes.push({
				path:absolutePath,
				component:() => import('../../doc/'+path+'/index.js')
			});
		}
	}
	return routes;
}

const router=new VueRouter({
	routes:[
		{
			path:'/',
			component:Main,
			children:[
				...getRoutes(pc,'pc/'),
				...getRoutes(app,'app/'),
			]
		},
	],
	base:'/',
	scrollBehavior(to,from,savedPosition){

	}
});
export default router;
